package com.kyh.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

public class MapUtil {

	
	//Map转Object
	public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
	    if (map == null)
	        return null;
	    Object obj = beanClass.newInstance();
	    Field[] fields = obj.getClass().getDeclaredFields();
	    for (Field field : fields) {
	        int mod = field.getModifiers();
	        if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
	            continue;
	        }
	        field.setAccessible(true);
	        if (map.containsKey(field.getName())) {
	        	Object value = map.get(field.getName());
	        	if (value != null) {
	        		if (field.getType() == Integer.class) {
			            field.set(obj, Integer.parseInt(value.toString()));
					} else if (field.getType() == Long.class) {
			            field.set(obj, Long.parseLong(value.toString()));
					} else if (field.getType() == BigDecimal.class) {
			            field.set(obj, new BigDecimal(value.toString()));
					} else if (field.getType() == String.class) {
			            field.set(obj, value.toString());
					} else if (field.getType() == Boolean.class) {
			            field.set(obj, value.toString());
					} else if (field.getType() == Date.class) {
			            field.set(obj, (Date)value);
					}
				}
	        }
	    }
	    return obj;
	}
}
